import 'package:flutter/material.dart';
import 'package:flutter_blank/common/app_colors.dart';
import 'package:flutter_blank/common/assets.dart';
import 'package:flutter_blank/pages/welcome.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  final duration = 10;
  int number = 10;

  Future<void> _countDown() async {
    while (number > 0) {
      number = duration;
      for (int i = 0; i < duration; i++) {
        await Future.delayed(const Duration(seconds: 1), () {
          if (mounted == true) {
            setState(() {
              number--;
            });
          }
        });
        // 倒计时结束, 进入 welcome
        if (number == 0) {
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(builder: (context) => const WelcomePage()),
          );
        }
      }
    }
  }

  @override
  void initState() {
    super.initState();
    _countDown();
  }

  Widget _buildLogo() {
    return Stack(
      alignment: Alignment.center,
      children: [
        // 底部
        Container(
          width: 120,
          height: 120,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(120 / 2),
          ),
        ),
        // 图标
        Image.asset(AssetsImages.logoPng, width: 84, height: 80),
      ],
    );
  }

  Text _buildText(String text) {
    return Text(
      text,
      style: const TextStyle(
        color: Colors.white,
        fontSize: 19,
        fontWeight: FontWeight.bold,
        // fontFamily: 'Poppins',
      ),
    );
  }

  // 主视图
  Widget _buildView(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 图标
          _buildLogo(),
          // 间距
          const SizedBox(height: 24),
          // 文字
          _buildText('Online Market'),
          // 间距
          const SizedBox(height: 24),
          // 计时器
          _buildText('$number'),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundSplash,
      body: _buildView(context),
    );
  }
}
